package bday.tvlport.beans;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import bday.tvlport.db.DbConnection;
import bday.tvlport.models.UserDetails;
import bday.tvlport.utils.DobUtils;
import bday.tvlport.utils.LoggerUtility;

@ManagedBean(name="user")
@SessionScoped
public class UserController {

	private String userId;
	private String name;
	private String email;
	private Integer dobMonth;
	private Integer dobDate;
	private List<String> dates;
	private static Map<String, Integer> monthMap = new LinkedHashMap<String, Integer>();  
	
	@ManagedProperty("#{returnMsg}")
	private String returnMsg;
	
	static {
		monthMap.put("Jan",1);
        monthMap.put("Feb",2);
        monthMap.put("Mar",3);
        monthMap.put("Apr",4);
        monthMap.put("May",5);
        monthMap.put("Jun",6);
        monthMap.put("Jul",7);
        monthMap.put("Aug",8);
        monthMap.put("Sep",9);
        monthMap.put("Oct",10);
        monthMap.put("Nov",11);
        monthMap.put("Dec",12);
	}
	
	public UserController() {
		
	}
	public UserController(String userId, String name, String email, String dobMonth, String dobDate) {
		this.userId = userId;
		this.name = name;
		this.email = email;
		this.dobMonth = monthMap.get(dobMonth);
		try {
			this.dobDate = Integer.valueOf(dobDate);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
	}
	
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getDobMonth() {
		if(dobMonth == null) {
			return null;
		}
		return dobMonth + "";
	}
	public void setDobMonth(String dobMonth) {
		this.dobMonth = monthMap.get(dobMonth);
	}
	public String getDobDate() {
		return dobDate + "";
	}
	public void setDobDate(String dobDate) {
		try {
			this.dobDate = Integer.valueOf(dobDate);
		} catch (NumberFormatException e) {
			LoggerUtility.error(e.toString());
		}
	}
	
	public List<String> getMonths() {
		List<String> months = new ArrayList<String>();
		months.add("Select Month");
		months.addAll(monthMap.keySet());
		return months;
	}
	
	public List<String> getDates() {
		dates = new ArrayList<String>();
		populateDates();
		return dates;
	}
	
	public String getReturnMsg() {
		return returnMsg;
	}
	public void setReturnMsg(String returnMsg) {
		this.returnMsg = returnMsg;
	}
	public void handleMonthChange() {
		populateDates();
	}
	private void populateDates() {
		dates = new ArrayList<String>();
		dates.add("Select Date");
		if (dobMonth != null) {
			int numDays = 29;
			switch (dobMonth) {
			case 1:
				numDays = 31;
				break;
			case 3:
				numDays = 31;
				break;
			case 4:
				numDays = 30;
				break;
			case 5:
				numDays = 31;
				break;
			case 6:
				numDays = 30;
				break;
			case 7:
				numDays = 31;
				break;
			case 8:
				numDays = 31;
				break;
			case 9:
				numDays = 30;
				break;
			case 10:
				numDays = 31;
				break;
			case 11:
				numDays = 30;
				break;
			case 12:
				numDays = 31;
				break;
			}
			for (int i = 1; i < numDays + 1; i++) {
				dates.add(i + "");
			}
		}
	}
	
	public String handleUser() {
		returnMsg = "error";
		try{
			MongoOperations mongoOperations = DbConnection.getMongo();
			//System.out.println("mongo: "+mongoOperations);
			if(mongoOperations != null) {
				String id = UUID.randomUUID().toString().replace("-", "");
				UserDetails userDetails = new UserDetails(id, userId, name, email, DobUtils.getMonthDate(dobMonth, dobDate));
				mongoOperations.save(userDetails);

				Query searchUserQuery = new Query(Criteria.where("email").is(email));
				
				UserDetails savedUser = mongoOperations.findOne(searchUserQuery, UserDetails.class);
				name = "No user found";
				if(savedUser != null) {
					name = savedUser.toString();
				}
				returnMsg = "success";
			}
		} catch (Throwable e) {
			reset();
			LoggerUtility.error(e.toString());
		}			
		return returnMsg;
	}
	
	public String getUser() {
		String ret = name;
		reset();
		return ret;
	}
	private void reset() {
		this.dobDate = null;
		setEmail(null);
		setName(null);
		setUserId(null);
		this.dates = null;
		this.dobMonth = null;
	}
}
